<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1{
            width: 200px;
            height: 200px;
            background-color: blue;
        }
        .box2{
            width: 100px;
            height: 100px;
            background-color: green;
        }
    </style>
</head>
<body>
    <button id="btn">按钮</button>
    <a href="https://www.bilibili.com/" id="lianjie">bilibili</a>
    <div class="box1" id="father">
        <div class="box2" id="son"></div>
    </div>

    <script>
        var btn = document.getElementById("btn");
        var btn1 = document.getElementById("lianjie");
        var box1 = document.getElementById("father");
        var box2 = document.getElementById("son");

        //event事件对象，作为function函数的参数
        btn.onclick = function(event){
            console.log(event);
            console.log(event.target);
            console.log(event.type);
            event.target.innerHTML = "点击之后";
        }

        btn1.onclick = function(){
            console.log("点击了链接");
            //阻止默认事件的发生，如此处阻止了跳转
            event.preventDefault();
        }

        box1.onclick = function(){
            console.log("点击了父元素");
        }
        box2.onclick = function(){
            console.log("点击了子元素");
            //阻止事件冒泡，即事件只会在该节点发生一次
            //如原本点击子元素也会触发父元素的事件，该方法可避免除当前节点外的事件发生
            event.stopPropagation();
        }

    </script>
</body>
</html>